<!DOCTYPE html>
<h4 id="test"></h4>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/user-gesture-utils.js"></script>
<script src="../resources/visibility.js"></script>
<script src="vibration-utils.js"></script>
<script>
'use strict';
// Simulates a user click for vibrate to be allowed.
var element = document.getElementById("test");
simulateUserClick(element.offsetLeft + 2, element.offsetTop + 2);

var t = async_test('Vibration should not start and it should cancel when the page is hidden.');

// Initially the page is hidden and no vibration can be started.
function test1() {
  assert_equals(document.visibilityState, 'hidden');
  assert_false(navigator.vibrate(1001));
  assert_array_equals(internals.pendingVibrationPattern(navigator), []);
  assert_false(internals.isVibrating(navigator));

  setMainWindowHidden(false).then(() => { t.step(test2); });
}

// Once the page is visible a vibration can be started.
function test2() {
  assert_equals(document.visibilityState, 'visible');
  assert_true(navigator.vibrate(1002));
  assert_array_equals(internals.pendingVibrationPattern(navigator), [1002]);
  assert_true(internals.isVibrating(navigator));

  setMainWindowHidden(true).then(() => { t.step(test3); });
}

function test3() {
  assert_equals(document.visibilityState, 'hidden');
  assert_array_equals(internals.pendingVibrationPattern(navigator), []);
  assert_false(internals.isVibrating(navigator));

  t.done();
}

t.step(() => {
  setMainWindowHidden(true).then(test1);
});

</script>
